package com.sanery.dao;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.dao.inter.ListDao;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.sanery.entity.Wrong;

public class ListDaoImpl extends BaseDaoImpl implements ListDao {

	// 查询错误
	@Override
	public Map<Integer, Wrong> getList(HttpServletRequest request) {
		Map<Integer, Wrong> map = new HashMap<Integer, Wrong>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = (Connection) this.getConn(); // 连接数据库
			String sql = "select w.id, u.nickName, w.title, w.img, w.answer, w.tip, w.createtime, w.dealtime, w.dealtype from wrongs w join users u on u.id = w.userid";
			ps = (PreparedStatement) conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				map.put(rs.getInt(1), new Wrong(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),
						rs.getString(5), rs.getString(6), rs.getDate(7), rs.getDate(8), rs.getString(9)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeAll(rs, ps, conn);
		}
		return map;
	}

	// 更新错误
	public void updateWrong(List list) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = (Connection) this.getConn(); // 连接数据库
			String sql = "update wrongs set title=?,answer=?,img=?,createtime=?,dealtype=?,dealtime=?,tip=? where id=?";
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps.setString(1, (String) list.get(0));
			ps.setString(2, (String) list.get(1));
			ps.setString(3, (String) list.get(2));
			if (!list.get(3).equals("")) {
				ps.setDate(4, strToDate((String) list.get(3)));
			} else {
				ps.setObject(4, null);
			}
			ps.setString(5, (String) list.get(4));
			if (!list.get(5).equals("")) {
				ps.setDate(6, strToDate((String) list.get(5)));
			} else {
				ps.setObject(6, null);
			}
			ps.setString(7, (String) list.get(6));
			ps.setInt(8, Integer.parseInt((String) list.get(7)));
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeAll(rs, ps, conn);
		}
	}

	// String 转 java.sql.Date
	public java.sql.Date strToDate(String strDate) {
		String str = strDate;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date d = null;
		try {
			d = format.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		java.sql.Date date = new java.sql.Date(d.getTime());
		return date;
	}

	// 新增错误
	@Override
	public void addWrong(List list) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = (Connection) this.getConn(); // 连接数据库
			String sql = "insert into wrongs values(default,?,?,?,?,?,?,null,?)";
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps.setInt(1, (int) list.get(0));
			ps.setString(2, (String) list.get(1));
			ps.setString(3, (String) list.get(2));
			ps.setString(4, (String) list.get(3));
			ps.setString(5, (String) list.get(4));
			Date utilDate = new Date();
			ps.setDate(6, new java.sql.Date(utilDate.getTime()));
			ps.setString(7, (String) list.get(5));
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeAll(rs, ps, conn);
		}
	}

	// 删除错误
	@Override
	public void delWrong(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = (Connection) this.getConn(); // 连接数据库
			String sql = "delete from wrongs where id=?";
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeAll(rs, ps, conn);
		}
	}
}
